package com.example.d_thymeleaf_validator.controller;

import com.example.d_thymeleaf_validator.entity.User;
import com.example.d_thymeleaf_validator.validator.group.Old;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author zk
 * @date 2019-10-22 14:09
 */
@Controller
public class RegisterController {

    @RequestMapping("/register")
    public String register(@ModelAttribute User user) {
        return "register";
    }

    @RequestMapping("/doRegister")
    public ModelAndView doRegister(@Validated(value = Old.class) User user, BindingResult bindingResult) {
        ModelAndView mav = new ModelAndView();
        if (bindingResult.hasErrors()) {
            mav.setViewName("register");
        } else {
            mav.setViewName("success");
        }
        return mav;
    }
}
